﻿// See https://aka.ms/new-console-template for more information
using SL651Center2.x;
using SL651Center2.x.Config;
using SL651Center2.x.Enums;
using SL651Center2.x.Messages;
using SL651Center2.x.Send;
using System.Text;
using TouchSocket.Sockets;

Console.WriteLine(Convert.ToString(Convert.ToInt32("29", 16), 2).PadLeft(8, '0'));
Console.WriteLine(Convert.ToInt32("129", 16));
byte b = 1;
Convert.ToString(b, 2);


string s = "40";
b = byte.Parse(s, System.Globalization.NumberStyles.HexNumber);
b = byte.Parse(s);
Console.WriteLine(b);
Console.WriteLine(Helper.HEXToASCII(s));

Console.WriteLine("Hello, World!");



Console.WriteLine("请选择报文格式：0：HEX，1：ASCII");
var framecode = Console.ReadLine();
CentralConfig config = new CentralConfig()
{
    CentralAddress = "0A",
    PassWord = "1234"
};
config.FrameCode = framecode == "0" ? FrameCode.HEX＿BCD : FrameCode.ASCII;

TcpServer tcpServer = new TcpServer();
tcpServer.Start();


Class1.Loop();
var cn = Console.ReadLine();

while (!string.IsNullOrEmpty(cn) && cn != "0")
{
    var ids = tcpServer.GetClientIds();

    Console.WriteLine("选择要发送的客户端:");
    for (int i = 0; i < ids.Length; i++)
    {
        Console.WriteLine($"{i}:客户端ID{ids[i]}");
    }
    var c = Console.ReadLine();
    int ci = Convert.ToInt32(c);
    var cid = ids[ci];
    var client = tcpServer.GetClient(cid);

    //获取遥测站信息
    TelemetryInfo telemetryInfo = config.GetTelemetryInfo("0012345678");

    //消息字节数组
    byte[]? msg = null;
    //水泵、阀门、闸门开关数据
    bool[] bools = new bool[8] { true, true, false, false, false, false, false, false };
    switch (cn)
    {
        case "1"://查询图片
            msg = CreateSend.QueryImgData(telemetryInfo);
            break;
        case "2"://查询实时数据
            msg = CreateSend.QueryRealTimeData(telemetryInfo);
            break;
        case "3"://查询时段数据
            msg = CreateSend.QueryTimePeriodData(telemetryInfo, DateTime.Now.AddHours(-1), DateTime.Now, "000010", ElementCode.Z);
            break;
        case "4"://查询人工置数
            msg = CreateSend.QueryArtificialData(telemetryInfo);
            break;
        case "5"://查询指定要素
            ElementCode[] els = new ElementCode[] { ElementCode.PJ, ElementCode.M10, ElementCode.VT };
            msg = CreateSend.QueryElementData(telemetryInfo, ElementCode.PJ, ElementCode.VT);
            break;
        case "6"://修改基本配置
            BasicConfigData[] dats = new BasicConfigData[]
            {
                new BasicConfigData()
                {
                    Code=BasicConfigCode.BC_01,
                    Data="0A000000"
                },new BasicConfigData()
                {
                    Code = BasicConfigCode.BC_02,
                    Data="001234567A"
                },new BasicConfigData(){
                    Code=BasicConfigCode.BC_03,
                    Data="1234"
                }
            };
            msg = CreateSend.SetBasicConfig(telemetryInfo, dats);
            break;
        case "7"://查询基本配置
            BasicConfigCode[] codes = new BasicConfigCode[] {
                BasicConfigCode.BC_01, BasicConfigCode.BC_02,
                BasicConfigCode.BC_03, BasicConfigCode.BC_0C };
            msg = CreateSend.QueryBasicConfig(telemetryInfo, codes);
            break;
        case "8"://修改运行配置
            RunConfigData[] datas = new RunConfigData[]
            {
                new RunConfigData()
                {
                    Code=RunConfigCode.RC_20,
                    Value=1
                },new RunConfigData()
                {
                    Code=RunConfigCode.RC_21,
                    Value=10
                },new RunConfigData(){
                    Code =RunConfigCode.RC_22,
                    Value=9
                }
            };
            msg = CreateSend.SetRunConfig(telemetryInfo, datas);
            break;
        case "9"://查询运行配置
            msg = CreateSend.QueryRunConfig(telemetryInfo, RunConfigCode.RC_20, RunConfigCode.RC_21);
            break;
        case "10"://查询水泵电机
            msg = CreateSend.QueryWaterPumpMotorData(telemetryInfo);
            break;
        case "11"://查询版本
            msg = CreateSend.QuerySoftwareVer(telemetryInfo);
            break;
        case "12"://查询状态和报警
            msg = CreateSend.QueryState(telemetryInfo);
            break;
        case "13"://初始化存储
            msg = CreateSend.InitializeData(telemetryInfo);
            break;
        case "14"://恢复出厂
            msg = CreateSend.FactoryReset(telemetryInfo);
            break;
        case "15"://修改密码
            msg = CreateSend.ChangePassWord(telemetryInfo, "1234", "AAAA");
            break;
        case "16"://设置时钟
            msg = CreateSend.SetClock(telemetryInfo);
            break;
        case "17"://设置IC卡
            msg = CreateSend.SetIcCardState(telemetryInfo, true);
            break;
        case "18"://控制水泵
            msg = CreateSend.ControlPump(telemetryInfo, bools);
            break;
        case "19"://控制阀门            
            msg = CreateSend.ControlValve(telemetryInfo, bools);
            break;
        case "20"://控制闸门
            //闸门开度数据
            int[] ints = new int[8] { 1, 2, 0, 0, 0, 0, 0, 0 };
            msg = CreateSend.ControlGate(telemetryInfo, bools, ints);
            break;
        case "21"://控制水量定值
            msg = CreateSend.ControlWaterValue(telemetryInfo, true);
            break;
        case "22"://查询事件记录
            msg = CreateSend.QueryIncidentRecord(telemetryInfo);
            break;
        case "23"://查询时钟
            msg = CreateSend.QueryClock(telemetryInfo);
            break;
        default:
            break;
    }

    if (msg != null && msg.Length != 0)
    {
        client.Send(msg);
        if (config.FrameCode == FrameCode.ASCII)
        {
            Console.WriteLine($"发送成功;报文：{Encoding.ASCII.GetString(msg)},长度：{msg.Length}");
            Console.WriteLine($"十六进制：{BitConverter.ToString(msg)}");
        }
        else
        {
            var h = BitConverter.ToString(msg);
            Console.WriteLine($"发送成功；报文：{h},长度：{msg.Length}");
        }

        Console.WriteLine("发送报文：");
        foreach (var item in msg)
        {
            Console.Write($"{item}\t");
        }
        Console.WriteLine();

    }


    Class1.Loop();
    cn = Console.ReadLine();
}

tcpServer.Stop();


